using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Bookvar.UI.Converters
{
    public class ControlCenterConverter : BookvarConverter<ControlCenterConverter>, IMultiValueConverter
    {
        public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
        {
            if ( values.Length != 4 )
            {
                return null;
            }

            double width = (double) values[ 0 ] / 2;
            double height = (double) values[ 1 ] / 2;
            double horizontalOffset = (double) values[ 2 ];
            double verticalOffset = (double) values[ 3 ];
            if ( !double.IsNaN( width ) && !double.IsNaN( height ) )
            {
                Point center = new Point( width, height );
                if ( !double.IsNaN( horizontalOffset ) )
                {
                    center.X += horizontalOffset;
                }
                if ( !double.IsNaN( verticalOffset ) )
                {
                    center.Y += verticalOffset;
                }
                return center;
            }
            return null;
        }

        public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException();
        }
    }
}